package com.zj.osm.view.project.projectdetail.fragment.scheme;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.text.TextUtils;

import com.zj.osm.App;
import com.zj.osm.entity.db.DaoSession;
import com.zj.osm.entity.db.PowerSupplySchemeDao;
import com.zj.osm.entity.db.PowerSupplySchemeDetailsDao;
import com.zj.osm.entity.db.ProjectDao;
import com.zj.osm.entity.greendao.PowerSupplyScheme;
import com.zj.osm.entity.greendao.PowerSupplySchemeDetails;
import com.zj.osm.entity.greendao.Project;
import com.zj.osm.utils.Configuration;
import com.zj.utils.utils.utilcode.TimeUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 供电方案model
 *
 * @author jiang zhu on 2019/10/30
 */
class PowerSupplySchemeModel {

    private DaoSession daoSession = App.getInstances().getDaoSession();

    Project getProject(Long projectDetailsId) {
        return daoSession.getProjectDao().queryBuilder().where(ProjectDao.Properties.Id.eq(projectDetailsId)).unique();
    }

    PowerSupplyScheme getPowerSupplyScheme(Project project) {

        PowerSupplyScheme supplyScheme;

        PowerSupplyScheme powerSupplyScheme = daoSession.getPowerSupplySchemeDao().queryBuilder().where(PowerSupplySchemeDao.Properties.ProjectId.eq(project.getId())).unique();
        if (powerSupplyScheme != null) {
            supplyScheme = powerSupplyScheme;
        } else {
            PowerSupplyScheme powerSupplySchemes = new PowerSupplyScheme();
            powerSupplySchemes.setApplyCode("");
            powerSupplySchemes.setApplyType("");
            powerSupplySchemes.setUserCode("");
            powerSupplySchemes.setUserName("");
            powerSupplySchemes.setAddress("");
            powerSupplySchemes.setElectricityAddress("");
            powerSupplySchemes.setOperator("");
            powerSupplySchemes.setOperatorPortableTelephone("");
            powerSupplySchemes.setOperatorFixedPhone("");
            powerSupplySchemes.setPic("");
            powerSupplySchemes.setPicFixedPhone("");
            powerSupplySchemes.setPicPortableTelephone("");
            powerSupplySchemes.setSchemeExplain("计量方式:高供高计，非工业，CT变比40/5，PT变比10000/ 100,用户安装无功补偿装置，功率因数不低于0.85。检查员提供2019.2.20.\n" +
                    "\n" +
                    "方案说明:\n" +
                    "\n" +
                    "1、新建双电源630KVA+630KVA红号联体箱站1座，主电源为(快36#)备用电源为(快52#);两路电源均采用电缆进线方式\n" +
                    "\n" +
                    "2、新设4间隔箱式开闭站2座，按照所属区城分区标准，同步实施配网自动化。新放ZCYJY22-8.7/15KV-3*300电缆30米，新放用户电缆ZCYJLY22-8. 7/15KV-3*150电缆500米，寻址器7组，电缆过路保护管120米。\n" +
                    "\n" +
                    "3、资产分界点:以公用变电站(开闭站)电缆出线直接入户的，以电缆终端接点处为分界点，分界点及以上电源侧属于供电设施。\n" +
                    "\n" +
                    "4、方案批准后，用户自行办理电缆破路破绿及相关配电站所占地手续，方案涉及物料最终以设计为准。\n" +
                    "\n" +
                    "5、此项目位于陆路港物流园，属于国家省市级园区项目，故此方案为业扩配套电网接入方案\n" +
                    "\n" +
                    "客户经理提供2019.2.21");
            powerSupplySchemes.setOpinion("");
            powerSupplySchemes.setProjectCode("");
            powerSupplySchemes.setDiagram("");
            powerSupplySchemes.setProjectName(project.getName());
            powerSupplySchemes.setOpinionTime(String.valueOf(System.currentTimeMillis()));
            powerSupplySchemes.setProjectId(project.getId());
            daoSession.getPowerSupplySchemeDao().insert(powerSupplySchemes);
            supplyScheme = powerSupplySchemes;
        }

        return supplyScheme;
    }

    List<PowerSupplySchemeDetails> getPowerSupplySchemeModelList(Long id) {
        return daoSession.getPowerSupplySchemeDetailsDao().queryBuilder().where(PowerSupplySchemeDetailsDao.Properties.PowerSupplySchemeId.eq(id)).list();
    }

    PowerSupplyScheme getPowerSupplySchemeById(Long projectId) {
        return daoSession.getPowerSupplySchemeDao().queryBuilder().where(PowerSupplySchemeDao.Properties.ProjectId.eq(projectId)).unique();
    }

    @SuppressLint("SimpleDateFormat")
    HashMap<String, Object> createWordFile(PowerSupplyScheme powerSupplySchemes, List<PowerSupplySchemeDetails> powerSupplySchemeDetailsList) {
        //创建生成的文件

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("APPLYCODE", powerSupplySchemes.getApplyCode());
        map.put("USERCODE", powerSupplySchemes.getUserCode());
        map.put("USERNAME", powerSupplySchemes.getUserName());
        map.put("projectName", powerSupplySchemes.getProjectName());
        map.put("electricityAddress", powerSupplySchemes.getElectricityAddress());

        map.put("applyType", powerSupplySchemes.getApplyType());
        map.put("address", powerSupplySchemes.getAddress());
        map.put("operator", powerSupplySchemes.getOperator());
        map.put("operatorFixedPhone", powerSupplySchemes.getOperatorFixedPhone());
        map.put("operatorPortableTelephone", powerSupplySchemes.getOperatorPortableTelephone());


        map.put("pic", powerSupplySchemes.getPic());
        map.put("TELE", powerSupplySchemes.getPicFixedPhone());
        map.put("PHONE", powerSupplySchemes.getPicPortableTelephone());
        map.put("SHUO", powerSupplySchemes.getSchemeExplain());
        map.put("opinion", powerSupplySchemes.getOpinion());


        map.put("projectCode", powerSupplySchemes.getProjectCode());

        if (!TextUtils.isEmpty(powerSupplySchemes.getOpinionTime())) {
            map.put("time", TimeUtils.millis2String(Long.parseLong(powerSupplySchemes.getOpinionTime()), new SimpleDateFormat(Configuration.MY_SIMPLE_DATE_FORMAT)));
        } else {
            map.put("time", TimeUtils.millis2String(new Date().getTime(), new SimpleDateFormat(Configuration.MY_SIMPLE_DATE_FORMAT)));
        }
        if (TextUtils.isEmpty(powerSupplySchemes.getDiagram())) {
            map.put("\'$IMG$", "无图片");
        }
        if (powerSupplySchemeDetailsList.size() > 0) {

            for (int i = 1; i < powerSupplySchemeDetailsList.size() + 1; i++) {
                map.put("Type" + i, powerSupplySchemeDetailsList.get(i - 1).getSourceType());
                map.put("Level" + i, powerSupplySchemeDetailsList.get(i - 1).getVoltageLevel());
                map.put("Line" + i, powerSupplySchemeDetailsList.get(i - 1).getLineNumber());
                map.put("Run" + i, powerSupplySchemeDetailsList.get(i - 1).getRunMethod());
                map.put("Power" + i, powerSupplySchemeDetailsList.get(i - 1).getPowerSupplyCapacity());
                map.put("Import" + i, powerSupplySchemeDetailsList.get(i - 1).getImportLineMethod());
                map.put("\'$TXT" + i + "$", powerSupplySchemeDetailsList.get(i - 1).getSourceCode());
            }
        } else {

            for (int i = 1; i < 3; i++) {
                map.put("Type" + i, "");
                map.put("Level" + i, "");
                map.put("Line" + i, "");
                map.put("Run" + i, "");
                map.put("Power" + i, "");
                map.put("Import" + i, "");
                map.put("\'$TXT$" + i + "$", "");
            }


        }

        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/bhne/" + powerSupplySchemes.getProjectName());
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }

        return map;
    }
}
